package spring.cloud.tasks.executor.executor.waitingExecuteTasks;

import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import spring.cloud.tasks.common.task_item_allot.TaskItem;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@Setter
@Getter
public class TaskInfo {
    public static final String TASK_TYPE_ITEM_TASK = "item_task";
    public static final String TASK_TYPE_MESSAGE_TASK = "message_task";
    public static final String TASK_TYPE_BATCH_TASK = "batch_task";
    // 状态枚举值
    public static final int WAITING = 1;
    public static final int CANCELED = 2;
    public static final int RUNNING = 3;
    public int status = TaskInfo.WAITING;
    private String taskType;
    private String taskId;
    private String signature;//签名  签名一旦改变那么则需要重新进行任务分配
    private String taskInstanceId;
    private String taskTurnId;//轮次id=任务实例#时间戳 一个任务实例同一时刻只能跑一个，可以支持重跑。

    public String taskInstanceId() {
        return taskId + "/" + taskInstanceId + "/" + taskTurnId;
    }

    private List<TaskItem> taskItemList = new ArrayList<>();
    private int loadLevel;
    private Date nextFireDate = new Date();//指的是可以触发的时间
    long startTime;
    long endTime;
    public SlidingTimeTaskBucket slidingTimeTaskBucket;

    public boolean cancel() {
        if (this.status == TaskInfo.WAITING) {
            this.status = TaskInfo.CANCELED;
            this.slidingTimeTaskBucket.getTaskInfoList().remove(this);
            return true;
        }
        return false;
    }

    public boolean isCancelled() {
        return this.status == TaskInfo.CANCELED;
    }
}
